using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Data.Linq;
using MimeCloud.AssetManagement.Business.Interface;
using MimeCloud.AssetManagement.DataProvider.Sql.Entities;
using MimeCloud.AssetManagement.ProviderServices.Data;
using MimeCloud.Foundations.ObjectManagement;

namespace MimeCloud.AssetManagement.DataProvider.Sql
{
    public class Writer : IAssetWriterFactory
    {
        private static AssetManagerDataContext context
        {
            get
            {
                return Singleton<AssetManagerDataContext>.UserContextScoped;
            }
        }

        public void SetDebugLog(TextWriter logger)
        {
            context.Log = logger;
        }

        public void SaveChanges()
        {
            context.SubmitChanges();
        }
        
        
        public IAsset GetBlankAsset()
        {
            return new Asset();
        }

        public IAssetContent GetBlankAssetContent()
        {
            return new AssetContent();
        }

        public IAssetContentType GetBlankAssetContentType()
        {
            return new AssetContentType();
        }

        public ILanguage GetBlankLanguage()
        {
            return new Language();
        }

        public IRepository GetBlankRepository()
        {
            return new Repository();
        }

        public IMimeType GetBlankMimeType()
        {
            return new MimeType();
        }

        public bool SaveAsset(IAsset asset)
        {
            throw new System.NotImplementedException();
        }

        public bool SaveLanguage(ILanguage language)
        {
            var lang = language as Language;
            if (lang == null)
                throw new ArgumentException("parameter was not of db type");

            bool update = (language.ID != Int32.MinValue);

            if (update)
            {
                context.Languages.Attach(lang, true);
            }
            else
                context.Languages.InsertOnSubmit(lang);

            context.SubmitChanges(ConflictMode.ContinueOnConflict);

            if (!update)
                return (lang.ID != Int32.MinValue);
            else
                return true;
        }

        public bool SaveRepository(IRepository repository)
        {
            throw new System.NotImplementedException();
        }

        public bool SaveAssetContentType(IAssetContentType contentType)
        {
            var contType = contentType as AssetContentType;
            if (contType==null)
                throw new ArgumentException("parameter was not of db type");

            bool update = (contType.ID != Int32.MinValue);

            if (update)
                context.AssetContentTypes.Attach(contType, true);
            else
                context.AssetContentTypes.InsertOnSubmit(contType);

            context.SubmitChanges(ConflictMode.ContinueOnConflict);

            if (!update)
                return (contType.ID != Int32.MinValue);
            else
                return true;
        }

        public bool SaveMimeType(IMimeType mimeType)
        {
            throw new System.NotImplementedException();
        }
    }
}
